package Class;

import java.io.InputStream;
import java.lang.reflect.*;
import java.util.Scanner;

/**
 * @Author czl
 * @Date 2024-03-26
 */
public class MainTest1 {
    public static void main(String[] args) throws Exception {

        String paramStr = "" ;

        Class<String> clazz = String.class;

        Method[] methods = clazz.getDeclaredMethods();

        for (Method method : methods) {
            String string = Modifier.toString(method.getModifiers());
            String a = method.getReturnType().getSimpleName();
            String simpleName = method.getName();

            System.out.print(string + " "  + a + " "  + simpleName + "(");

            Parameter[] parameters = method.getParameters();
            for (Parameter parameter : parameters) {
                String simpleName1 = parameter.getType().getSimpleName();

                String name = parameter.getName();

                paramStr += simpleName1 + name;
            }
            paramStr = "".equals(paramStr) ? paramStr : paramStr.substring(0,paramStr.length()-1);
            System.out.print(paramStr);

            System.out.println(")");

            paramStr = "" ;
        }



        //实例化对象，调用 study 方法，打印输出返回的经验值
        Class<?> clazz1 = Class.forName("Class.Student");
        Object obj2 = clazz1.getConstructor(String.class, Double.class).newInstance("傻椿", 100.0);
        Method declaredMethods = clazz1.getDeclaredMethod("study",String.class,Double.class);
        declaredMethods.setAccessible(true);
        System.out.println(declaredMethods.invoke(obj2,"教室", 10.0));



        //使用反射机制来实现用户输入一个年份，判断是闰年还是平年
        Class<?> clazz3 = Scanner.class;
        Object obj3 = clazz3.getConstructor(InputStream.class).newInstance(System.in);
        System.out.print("请输入一个年份:");
        Method method = clazz3.getMethod("nextInt");
        int year = (int)method.invoke(obj3);
        if (year % 4 == 0 || year % 100 == 0 || year % 400 == 0) {
            System.out.println(year+"是闰年");
            return;
        }
        System.out.println(year+"是平年");



        //输入一个合法的Email地址，截取其帐户名
        String email = "zhangsan@qq.com" ;

        Class<String> clazz4 = String.class;

        Method substring = clazz4
                .getMethod("substring",int.class,int.class);

        Method indexOf = clazz4.getMethod("indexOf",String.class);

        // 1.获取 @ 符号的下标索引
        Integer index = (Integer)indexOf.invoke(email, "@");

        // 2.截取字符串
        Object user = substring.invoke(email, 0, index);
        System.out.println(user);
    }
}
